package com.java110.mall.cart.cmd.index;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderCartReturnInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.shop.IShopVisitInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.ArrayList;
import java.util.List;

@Java110Cmd(serviceCode = "/index/getTodayStoreOrder")
public class GetTodayStoreOrderCmd extends Cmd {

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartReturnInnerServiceSMO storeOrderCartReturnInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IShopVisitInnerServiceSMO shopVisitInnerServiceSMO;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson,"shopId","未包含商铺信息");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException {

        //查询今天订单数
        StoreOrderDto tmpStoreOrderDto = BeanConvertUtil.covertBean(reqJson,StoreOrderDto.class);
        tmpStoreOrderDto.setShopId(reqJson.getString("shopId"));
        tmpStoreOrderDto.setStates(new String[]{StoreOrderDto.STATE_FINISH_PAY,
                StoreOrderDto.STATE_WAIT_SEND,
                StoreOrderDto.STATE_WAIT_RECIVE,
                StoreOrderDto.STATE_WAIT_REPAIR,
                StoreOrderDto.STATE_APPLY_RETURN
        });
        tmpStoreOrderDto.setStartTime(DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_B));


        int count = storeOrderInnerServiceSMOImpl.queryStoreOrdersCount(tmpStoreOrderDto);

        List<StoreOrderDto> storeOrderDtos = null;
        if (count > 0) {
            storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(tmpStoreOrderDto);
        } else {
            storeOrderDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) tmpStoreOrderDto.getRow()), count, storeOrderDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        cmdDataFlowContext.setResponseEntity(responseEntity);

    }


}
